package service;

import dao.PatientDao;
import dao.RegRecordDao;
import model.Patient;
import model.RegRecord;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class PatientService {

    /**
     * 病人注册
     */
    public static boolean regist(Integer id,String name,String password,Integer sex,Integer age){
        List<Patient> models = PatientDao.select(id,null,null,null,null);
        boolean flag = false;
        if(models.size()>0){
            System.out.println("病人账号[" + id + "]已存在。");
        }else {
            Patient model = new Patient(id,name,sex,age,password);
            PatientDao.insert(model);
            System.out.println("病人注册成功。");
            flag = true;
        }
        return flag;
    }

    /**
     * 病人登陆
     */
    public static Patient login(String id,String password){
        Patient model = null;
        List<Patient> models = PatientDao.select(Integer.valueOf(id),null,null,null,password);
        if(models.size()>0){
            model = models.get(0);
            System.out.println("病人[" + model.getName() + "]登陆成功。");
        }else {
            System.out.println("病人账号或密码错误，登陆失败。");
        }
        return model;
    }

    /**
     * 病人挂号
     */
    public static boolean sendRegRecord(Integer patientId, String deptId) {
        try {
            Date curTime = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("HH");
            int hour = Integer.valueOf(sdf.format(curTime));
            int price = 10;
            if(hour > 17 || hour < 9){
                price = 20;
            }
            RegRecord regRecord = new RegRecord(null, patientId, Integer.valueOf(deptId), curTime, new BigDecimal(price));
            return RegRecordDao.insert(regRecord);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
